﻿"""Add project templates and members tables

Revision ID: 20241002_project_templates_members
Revises: 7bb3828fa69a
Create Date: 2025-10-02 13:30:00

"""
# pylint: skip-file
# type: ignore
# pyright: reportAttributeAccessIssue=false, reportGeneralTypeIssues=false
from typing import Sequence, Union

from alembic import op  # type: ignore[import]
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = "20241002_project_templates_members"
down_revision: Union[str, Sequence[str], None] = "7bb3828fa69a"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


PROJECT_MEMBER_ROLE = sa.Enum(
    "owner",
    "project_manager",
    "consultant",
    "viewer",
    name="project_member_role",
)

PROJECT_TYPE = sa.Enum(
    "automation",
    "integration",
    "data",
    "ai",
    "custom",
    name="project_type",
    create_type=False,
)


def upgrade() -> None:
    """Upgrade schema."""
    bind = op.get_bind()
    PROJECT_MEMBER_ROLE.create(bind, checkfirst=True)

    op.create_table(
        "project_templates",
        sa.Column("id", sa.UUID(as_uuid=True), primary_key=True),
        sa.Column("name", sa.String(length=120), nullable=False, unique=True),
        sa.Column("description", sa.Text(), nullable=True),
        sa.Column("project_type", PROJECT_TYPE, nullable=False),
        sa.Column("default_budget", sa.String(length=64), nullable=True),
        sa.Column("default_deadline_offset_days", sa.Integer(), nullable=True),
        sa.Column("default_description", sa.Text(), nullable=True),
        sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.text("(CURRENT_TIMESTAMP)"), nullable=False),
        sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.text("(CURRENT_TIMESTAMP)"), nullable=False),
    )

    op.create_table(
        "project_members",
        sa.Column("id", sa.Integer(), primary_key=True),
        sa.Column("project_id", sa.Integer(), sa.ForeignKey("projects.id", ondelete="CASCADE"), nullable=False),
        sa.Column("user_id", sa.Integer(), sa.ForeignKey("users.id", ondelete="CASCADE"), nullable=False),
        sa.Column("role", PROJECT_MEMBER_ROLE, nullable=False, server_default="consultant"),
        sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.text("(CURRENT_TIMESTAMP)"), nullable=False),
        sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.text("(CURRENT_TIMESTAMP)"), nullable=False),
        sa.UniqueConstraint("project_id", "user_id", name="uq_project_member"),
    )
    op.create_index(op.f("ix_project_members_project_id"), "project_members", ["project_id"], unique=False)
    op.create_index(op.f("ix_project_members_user_id"), "project_members", ["user_id"], unique=False)


def downgrade() -> None:
    """Downgrade schema."""
    op.drop_index(op.f("ix_project_members_user_id"), table_name="project_members")
    op.drop_index(op.f("ix_project_members_project_id"), table_name="project_members")
    op.drop_table("project_members")
    op.drop_table("project_templates")
    PROJECT_MEMBER_ROLE.drop(op.get_bind(), checkfirst=True)

